package com.zzyl.plan.service.impl;

import java.util.List;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.dto.NursingProjectPlanDto;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;

import com.zzyl.nursing.service.INursingPlanService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-21
 */
@Service
public class NursingPlanServiceImpl implements INursingPlanService 
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Integer id)
    {
        //1.查询护理计划(基础信息)
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        //2.查询护理计划和护理项目关联关系
        List<NursingProjectPlanVo>list = nursingProjectPlanMapper.selectByPlanId(id);
        //3.组装
        NursingPlanVo vo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan,vo);
        vo.setId(id.longValue());
        vo.setProjectPlans(list);
        return vo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param dto 护理计划
     * @return 结果
     */
    @Transactional
    @Override
    public int insertNursingPlan(NursingPlanDto dto )
    {
        //1.保存护理计划(基础信息)
        NursingPlan nursingPlan = new NursingPlan();
        //属性名和类型须一致
        BeanUtils.copyProperties(dto,nursingPlan);
        //创建时间
        nursingPlan.setCreateTime(DateUtils.getNowDate());

        nursingPlanMapper.insertNursingPlan(nursingPlan);

        //2.批量保存护理计划和护理信息的关系
        int row = nursingProjectPlanMapper.batchInsert(nursingPlan.getId(), dto.getProjectPlans());
        return row == 0 ? 0 : 1;
    }

    /**
     * 修改护理计划
     * 
     * @param nursingPlan 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan(NursingPlanDto dto) {
        try {
            //1.修改护理计划(基础信息)
            NursingPlan nursingPlan = new NursingPlan();
            BeanUtils.copyProperties(dto,nursingPlan);
            nursingPlan.setId(dto.getId().intValue());
            //2.判断DTO中是否有项目信息
            List<NursingProjectPlanDto> list = dto.getProjectPlans();
            if(list != null && !list.isEmpty()){
                //中间表先删再加
                nursingProjectPlanMapper.deleteByPlanId(nursingPlan.getId());
                nursingProjectPlanMapper.batchInsert(nursingPlan.getId(), list);
            }
            return nursingPlanMapper.updateNursingPlan(nursingPlan);
        } catch (BeansException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Integer[] ids)
    {
        return nursingPlanMapper.deleteNursingPlanByIds(ids);
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Integer id)
    {
        try {
            nursingProjectPlanMapper.deleteByPlanId(id);
            return nursingPlanMapper.deleteNursingPlanById(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
